package com.web.kftsdk.common.bean;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 *
 * @author xukk
 * @date 2018/5/4
 */
@Data
@Builder
@AllArgsConstructor
public class PayRes<T> implements Serializable {
    /**
     * 900000成功，非900000失败
     **/
    private int status;
    /**
     * 详情
     */
    private String message;

    /**
     * 错误类型
     */
    private String error;

    private String errorDescription;

    private String path;

    /**
     * 内容
     */
    protected T data;
    /**
     * 时间戳
     */
    private long timestamp;

    public PayRes() {
        this.timestamp = System.currentTimeMillis();
    }

    public PayRes failure() {
        this.status = PayStatus.FAIL.code();
        this.message = PayStatus.FAIL.desc();
        this.error = PayStatus.FAIL.name();
        this.errorDescription = this.message;
        this.timestamp = System.currentTimeMillis();
        return this;
    }
    public PayRes failure(PayStatus PayStatus){
        this.status = PayStatus.code();
        this.message = PayStatus.desc();
        this.error = PayStatus.name();
        this.errorDescription = PayStatus.desc();
        this.timestamp = System.currentTimeMillis();
        return this;
    }
    public PayRes failure(PayStatus PayStatus, String message){
        this.status = PayStatus.code();
        this.message =message;
        this.error = PayStatus.name();
        this.errorDescription = message;
        this.timestamp = System.currentTimeMillis();
        return this;
    }
    public PayRes failure(Integer status, String message) {
        this.status = status;
        this.message = message;
        this.error = PayStatus.FAIL.name();
        this.errorDescription = this.message;
        this.timestamp = System.currentTimeMillis();
        return this;
    }

    public PayRes failure(String message) {
        this.status = PayStatus.FAIL.code();
        this.message = message;
        this.error = PayStatus.FAIL.name();
        this.errorDescription = this.message;
        this.timestamp = System.currentTimeMillis();
        return this;
    }

    public PayRes failure(String error, String message) {
        this.status = PayStatus.FAIL.code();
        this.message = message;
        this.error = error;
        this.errorDescription = this.message;
        this.timestamp = System.currentTimeMillis();
        return this;
    }
    public PayRes path(String path) {
        this.path=path;
        return this;
    }
    public PayRes failure(Integer status, String error, String message) {
        this.status = status;
        this.message = message;
        this.error = error;
        this.errorDescription = this.message;
        this.timestamp = System.currentTimeMillis();
        return this;
    }

    public PayRes success() {
        this.status = PayStatus.SUCCESS.code();
        this.message = PayStatus.SUCCESS.desc();
        this.error =  "";
        this.errorDescription =  PayStatus.SUCCESS.desc();
        this.timestamp = System.currentTimeMillis();
        return this;
    }

    public PayRes success(String message) {
        this.status = PayStatus.SUCCESS.code();
        this.message = message;
        this.error = "";
        this.errorDescription = "";
        this.timestamp = System.currentTimeMillis();
        return this;
    }

    public Boolean valid() {
        if (status == PayStatus.SUCCESS.code()) {
            return true;
        } else {
            return false;
        }
    }

    public static PayRes Custom() {
        PayRes PayRes = new PayRes();
        return PayRes;
    }
    public static PayRes Str() {
        PayRes<String> PayRes = new PayRes<>();
        if (PayRes.getData() == null) {
            PayRes.setData("");
        }
        return PayRes;
    }
    public static PayRes List() {
        PayRes<List> PayRes = new PayRes<List>();
        if (PayRes.getData() == null) {
            PayRes.setData(Lists.newArrayList());
        }
        return PayRes;
    }

    public static PayRes Map() {
        PayRes<Map> PayRes = new PayRes<Map>();
        if (PayRes.getData() == null) {
            PayRes.setData(Maps.newHashMap());
        }
        return PayRes;
    }
    public PayRes add(Object value) {
        if (this.getData() == null) {
           List list= Lists.newArrayList();
            list.add(value);
            this.data=(T)list;
        }else {
            List list=(List)this.getData();
            list.add(value);
        }
        return this;
    }
    public PayRes put(String key, Object value) {
        if (this.getData() == null) {
            Map map= Maps.newHashMap();
            map.put(key, value);
            this.data=(T)map;
        }else {
            Map map=(Map)this.getData();
            map.put(key, value);
        }
        return this;
    }

    public PayRes putAll(Map map) {
        if (this.getData() == null) {
           this.data=(T)map;
        }else {
            Map map1=(Map)this.getData();
            map1.putAll(map);
        }
        return this;
    }
    @SuppressWarnings("unchecked")
    public PayRes addData(T data) {
        this.data = data;
        return this;
    }
}